package com.suning.sawp.web.controller.member;

import java.security.Principal;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.suning.sawp.constants.ErrorCodeConstants;
import com.suning.sawp.constants.LoggerConstants;
import com.suning.sawp.dto.common.ReturnMsg;
import com.suning.sawp.dto.mainpush.StoreManDto;
import com.suning.sawp.intf.storeman.StoreManService;
import com.suning.sawp.member.service.StoreExpandService;
import com.suning.sawp.member.vo.CateExpandDetailVo;
import com.suning.sawp.member.vo.EmpExpandDetailVo;
import com.suning.sawp.member.vo.StoreExpandDetailVo;
import com.suning.sawp.web.controller.DjBaseController;
import com.suning.sawp.web.exception.MyGsonView;
/**
 * 
 * 门店外拓控制层<br> 
 *
 * @author 14060886 祝翔龙
 */
@Controller
@RequestMapping("/client/activity")
public class StoreExpandController extends DjBaseController {
    
    private static final Logger LOGGER = LoggerFactory.getLogger(LoggerConstants.SAWP_SERVICE);
    
    @Autowired
    private StoreExpandService storeExpandService;
    @Autowired
    private StoreManService storeManService;
    
    /**
     * 
     * 功能描述: 查询某门店外拓活动详情(品类分组)
     *
     * @param activityId
     * @param dateType
     * @return
     */
    @RequestMapping("/queryExpandDetailByStore.do")
    public MyGsonView queryExpandDetailByStore(Principal principal, String activityId, 
            @RequestParam(defaultValue = "0", required = false) String dateType) {
        LOGGER.info("queryExpandDetailByStore in, principal={}, activityId={}, dateType={}", principal.getName(), activityId, dateType);
        ReturnMsg<StoreExpandDetailVo> retMsg = new ReturnMsg<StoreExpandDetailVo>();
        if (isFieldNull(activityId)) {
            return getErrorGsonView(retMsg);
        }
        // 工号
        String staffId = principal.getName();
        // 查询店员信息
        StoreManDto storeMan = storeManService.queryStoreManAttachInfo(staffId);
        if(null == storeMan){
            // 店员信息异常
            retMsg.setError(ErrorCodeConstants.ERR_MEM_0002);
            return setGsonViewWithEmpty(retMsg);
        }
        StoreExpandDetailVo storeExpandDetail = storeExpandService.queryExpandDetailByStore(activityId, storeMan.getStoreCode(), dateType);
        retMsg.setData(storeExpandDetail);
        return setGsonViewWithEmpty(retMsg);
    }
    
    /**
     * 
     * 功能描述: 查询某品类外拓活动详情(店员分组)
     *
     * @param activityId
     * @param cateCode
     * @return
     */
    @RequestMapping("/queryExpandDetailByCate.do")
    public MyGsonView queryExpandDetailByCate(Principal principal, String activityId, String cateCode) {
        LOGGER.info("queryExpandDetailByCate in, principal={}, activityId={}, cateCode={}",  principal.getName(), activityId, cateCode);
        ReturnMsg<CateExpandDetailVo> retMsg = new ReturnMsg<CateExpandDetailVo>();
        if (isFieldNull(activityId, cateCode)) {
            return getErrorGsonView(retMsg);
        }
        // 工号
        String staffId = principal.getName();
        // 查询店员信息
        StoreManDto storeMan = storeManService.queryStoreManAttachInfo(staffId);
        if(null == storeMan){
            // 店员信息异常
            retMsg.setError(ErrorCodeConstants.ERR_MEM_0002);
            return setGsonViewWithEmpty(retMsg);
        }
        CateExpandDetailVo cateExpandDetail = storeExpandService.queryExpandDetailByCate(activityId, storeMan.getStoreCode(), cateCode);
        retMsg.setData(cateExpandDetail);
        return setGsonViewWithEmpty(retMsg);
    }
    
    /**
     * 
     * 功能描述: 查询某地点外拓活动详情(店员分组)
     *
     * @param activityId
     * @param expandId
     * @return
     */
    @RequestMapping("/queryExpandDetailByLoc.do")
    public MyGsonView queryExpandDetailByLoc(Principal principal, String activityId, String expandId) {
        LOGGER.info("queryExpandDetailByLoc in, principal={}, activityId={}, expandId={}", principal.getName(), activityId, expandId);
        ReturnMsg<CateExpandDetailVo> retMsg = new ReturnMsg<CateExpandDetailVo>();
        if (isFieldNull(activityId, expandId)) {
            return getErrorGsonView(retMsg);
        }
        // 工号
        String staffId = principal.getName();
        // 查询店员信息
        StoreManDto storeMan = storeManService.queryStoreManAttachInfo(staffId);
        if(null == storeMan){
            // 店员信息异常
            retMsg.setError(ErrorCodeConstants.ERR_MEM_0002);
            return setGsonViewWithEmpty(retMsg);
        }
        CateExpandDetailVo cateExpandDetail = storeExpandService.queryExpandDetailByLoc(activityId, storeMan.getStoreCode(), expandId);
        retMsg.setData(cateExpandDetail);
        return setGsonViewWithEmpty(retMsg);
    }
    
    /**
     * 
     * 功能描述: 查询某店员外拓活动详情
     *
     * @param activityId
     * @param employeeId
     * @param expandId
     * @param startIndex
     * @return
     */
    @RequestMapping("/queryExpandDetailByEmp.do")
    public MyGsonView queryExpandDetailByEmp(Principal principal, String activityId, String employeeId, String expandId, 
            @RequestParam(defaultValue = "0", required = false) String startIndex) {
        LOGGER.info("queryExpandDetailByEmp in, principal={}, activityId={}, employeeId={}, expandId={}, startIndex={}", 
                principal.getName(), activityId, employeeId, expandId, startIndex);
        ReturnMsg<EmpExpandDetailVo> retMsg = new ReturnMsg<EmpExpandDetailVo>();
        if (isFieldNull(activityId, employeeId)) {
            return getErrorGsonView(retMsg);
        }
        // 工号
        String staffId = principal.getName();
        // 查询店员信息
        StoreManDto storeMan = storeManService.queryStoreManAttachInfo(staffId);
        if(null == storeMan){
            // 店员信息异常
            retMsg.setError(ErrorCodeConstants.ERR_MEM_0002);
            return setGsonViewWithEmpty(retMsg);
        }
        EmpExpandDetailVo empExpandDetail = storeExpandService.queryExpandDetailByEmp(activityId, storeMan.getStoreCode(), employeeId, 
                expandId, startIndex);
        retMsg.setData(empExpandDetail);
        return setGsonViewWithEmpty(retMsg);
    }
    
    @SuppressWarnings({ "rawtypes", "unchecked" })
    private MyGsonView getErrorGsonView(ReturnMsg retMsg){
        retMsg.setRetFlag(ReturnMsg.FAIL);
        retMsg.setErrorCode(ErrorCodeConstants.ERR_0001);
        retMsg.setErrorMessage(ErrorCodeConstants.ERR_MAP.get(ErrorCodeConstants.ERR_0001));
        return setGsonViewWithEmpty(retMsg);
    }
 
}
